﻿/*
 *   DateFunctionsTests.cs
 *   Copyright (c) 2011 - BitFox Software and Ricardo Federico Villafañes
 *   Contact: federvillaf@hotmail.com
 *   
 *   Licensing: This product is covered by Microsoft Reciprocal License (MS-RL)
 *              see https://bitfoxeval.codeplex.com/license
*/

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using BitFox.Evaluator;

namespace TestEvaluator
{
    /// <summary>
    /// Tests related, mainly, to numeric functions
    /// </summary>
    [TestClass]
    public class DateFunctionsTests
    {
        // The evaluator to use in tests
        Evaluator eval = new Evaluator();

        /// <summary>
        /// Empty constructor
        /// </summary>
        public DateFunctionsTests()
        {
        }

        /// <summary>
        /// The context under which tests are runned.
        ///</summary>
        public TestContext TestContext { get; set; }

        /// <summary>
        /// Test DATEPART fox's function
        /// </summary>
        [TestMethod]
        public void TestDATEPARTS()
        {
            Assert.AreEqual(6, (int)eval.Compile("dow({^2010/01/15})").Run());
            
            Assert.AreEqual("Friday", (string)eval.Compile("cdow({^2010/01/15})").Run());

            Assert.AreEqual(15, (int)eval.Compile("day({^2010/01/15})").Run());
            Assert.AreEqual(1, (int)eval.Compile("month({^2010/01/15})").Run());
            Assert.AreEqual(2010, (int)eval.Compile("year({^2010/01/15})").Run());

            Assert.AreEqual("January", (string)eval.Compile("cmonth({^2010/01/15})").Run());
        }

        /// <summary>
        /// Test CTOD fox's function
        /// </summary>
        [TestMethod]
        public void TestCTOD()
        {
            eval.Context.SetDate = SetDateOptions.French;
            Assert.AreEqual(new DateTime(2010, 3, 15), (DateTime)eval.Compile("CTOD('15/3/2010')").Run());
            Assert.AreEqual(new DateTime(2010, 3, 15), (DateTime)eval.Compile("CTOD('   15/3/2010')").Run());
            Assert.AreEqual(new DateTime(2010, 3, 15), (DateTime)eval.Compile("CTOD(' 15  /3/2010')").Run());
            Assert.AreEqual(new DateTime(2010, 3, 15), (DateTime)eval.Compile("CTOD('15/  3/  2010')").Run());
            Assert.AreEqual(new DateTime(2010, 3, 15), (DateTime)eval.Compile("CTOD('15/3/2010   ')").Run());
            Assert.AreEqual(new DateTime(2010, 3, 15), (DateTime)eval.Compile("CTOD('15/3/2010  x')").Run());
            Assert.AreEqual(new DateTime(2010, 3, 15), (DateTime)eval.Compile("CTOD(' 15 - 3  . 2010  x')").Run());
        }

        /// <summary>
        /// Test GOMONTH fox's function
        /// </summary>
        [TestMethod]
        public void TestGOMONTH()
        {
            Assert.AreEqual(new DateTime(2011, 8, 2), (DateTime)eval.Compile("GOMONTH({^2010/7/2}, 13)").Run());
            Assert.AreEqual(new DateTime(2009, 8, 2), (DateTime)eval.Compile("GOMONTH({^2010/7/2}, -11)").Run());
            Assert.AreEqual(new DateTime(2010, 8, 2), (DateTime)eval.Compile("GOMONTH({^2010/8/2}, 0)").Run());
        }
    }
}
